package com.ruoyi.web.controller.tax;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.AreaInfo;
import com.ruoyi.system.domain.Industry;
import com.ruoyi.system.domain.InstitutionInfo;
import com.ruoyi.system.service.IAreaInfoService;
import com.ruoyi.system.service.IRegisterUserService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.awt.geom.Area;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("tax")
public class AreaController extends BaseController {

    private String prefix = "system/tax";

    @Autowired
    public IAreaInfoService areaInfoService;

    List<AreaInfo> areaInfoList;

    List<Industry> industryList;

    public List<Industry> getIndustryList() {
        return industryList = areaInfoService.selectIndustryData();
    }

    public List<AreaInfo> getAreaInfoList(){

        return areaInfoList = areaInfoService.selectAreaData();
    }


    /**
     *获取地区 -1 代表一级
     */
    @GetMapping("/getAreaData/{parentCode}")
    @ResponseBody
    public List<AreaInfo> getCityData(@PathVariable("parentCode") String parentCode){
        if (CollectionUtils.isEmpty(areaInfoList)) {
            getAreaInfoList();
        }
        if ("-1".equals(parentCode)){
            return  areaInfoList.stream().filter((p) -> p.getParentid() == -1).collect(Collectors.toList());
        }else {
            return  areaInfoList.stream().filter((p) ->parentCode.equals(p.getParentcode())).collect(Collectors.toList());
        }
    }


    /**
     *获取行业 -1 代表一级
     */
    @GetMapping("/getIndustry/{parentCode}")
    @ResponseBody
    public List<Industry> getIndustry(@PathVariable("parentCode") String parentCode){
        if (CollectionUtils.isEmpty(industryList)) {
            getIndustryList();
        }
        if ("-1".equals(parentCode)){
            return  industryList.stream().filter((p) -> p.getCode().length() == 1).collect(Collectors.toList());
        }else {
            return  industryList.stream().filter((p) ->parentCode.equals(p.getParentCode())).collect(Collectors.toList());
        }
    }

}